package common.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.open4a.openstudy.entity.DaoMaster;
import com.open4a.openstudy.entity.DaoSession;


/**
 * Created by yangkuo02 on 2017/6/5.
 */

public class GreenDaoManager {

    public static final String DB_NAME = "open-db";

    private static volatile GreenDaoManager instance;

    private DaoMaster.DevOpenHelper helper;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public GreenDaoManager() {
    }

    public static GreenDaoManager getInstance() {
        if (instance == null) {
            synchronized (GreenDaoManager.class) {
                if (instance == null) {
                    instance = new GreenDaoManager();
                }
            }
        }
        return instance;
    }

    public void init(Context context) {
        helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }
}
